package com.zoe.onelink.core.web.jackson.serializer;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.zoe.onelink.core.web.jackson.JacksonConstant;
import com.zoe.onelink.util.DateUtil;

import java.io.IOException;
import java.util.Date;

/**
 * <p>标题: Jackson日期反序列化</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-06-23
 */
public class JacksonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String nodeStr = p.getText();
        // 如果参数为空 , 不属于异常 , 直接返回空对象
        if (StrUtil.isBlank(nodeStr)) {
            return null;
        }
        if (nodeStr.startsWith(JacksonConstant.QUOT) && nodeStr.endsWith(JacksonConstant.QUOT)) {
            nodeStr = nodeStr.substring(1, nodeStr.length() - 1);
        }
        return DateUtil.convert(nodeStr);
    }

}
